package controllers

import (
	"blog/models"
	"fmt"

	"github.com/astaxie/beego"
)

type FelleiController struct {
	beego.Controller
}

func (this *FelleiController) Get() {

	if checkAccount(this.Ctx) {
		this.Data["Cookie"] = true
	}
	fmt.Println("%s", this.Ctx.Input.RequestBody)
	op := this.GetString("op")
	switch op {
	case "add":
		name := this.GetString("uname")
		if name == "" {
			break
		}
		tubiao := this.GetString("tubiao")
		err := models.AddFenlei(tubiao, name)
		if err != nil {
			break
		}
		this.Redirect("/fenlei", 302)
		return
	case "addnav":
		id := this.GetString("id")

		fmt.Println("id :", id)
		err := models.UpdateFenlei(id, true)
		if err != nil {
			beego.Error(err)
			break
		}
		this.Redirect("/fenlei", 302)
		return
	case "delnav":
		id := this.GetString("id")
		err := models.UpdateFenlei(id, false)
		if err != nil {
			beego.Error(err)
			break
		}
		this.Redirect("/fenlei", 302)
		return
	}

	var err error
	this.Data["fenlei"], err = models.GetFenleis()
	if err != nil {
		beego.Error(err)

	}
	this.TplName = "feilei.html"
}

func (this *FelleiController) Query() {
	id := this.GetString("id")
	this.Data["ToPics"], _ = models.GetTopics()
	this.Data["fenlei"], _ = models.GetFenleis()
	this.Data["fenleiNumber"] = id
	this.TplName = "fenlei_index.html"

}
